// import { Common } from "./Common";
// import { EventManager } from "./EventManager";


import Global1 from "./Global1";
import ToastManager from "./ToastManager";


export let Http1 = {

    // 发送事件
    get(path, data, callback, target, errCB?) {
        var str = "?";
        data = data || {}

        for (var k in data) {
            if (str != "?") {
                str += "&";
            }
            str += k + "=" + data[k];
        }
        let url = Global1.baseUrl + path + str;
        // let url = path;
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if (xhr.status >= 200 && xhr.status < 400) {
                    let response = JSON.parse(xhr.responseText);
                    if (response.code == 200) {
                        callback.call(target, response.data);
                    } else {

                        if (errCB) errCB.call(target);
                        ToastManager.instance.show(`${response.msg}`)
                    }
                } else {
                    console.log("status:", xhr.status);
                }
                // GlobalBlock.instance.hide();
            }
        };
        xhr.onerror = () => {
            // GlobalBlock.instance.hide();

            // EventManager.sendEvent(Common.LIGHT_TIPS, {str:'网络错误'});
        }
        xhr.ontimeout = () => {
            // GlobalBlock.instance.hide();

            // EventManager.sendEvent(Common.LIGHT_TIPS, {str:'网络超时'});
        }
        xhr.open("GET", url, true);
        if (cc.sys.isNative) {
            xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
        }

        if (Global1.token && Global1.token != "") {
            xhr.setRequestHeader("Authorization", Global1.token);
        }

        xhr.send();

        // GlobalBlock.instance.show();
    },

    // 发送事件
    post(path, data, callback, target, errCB?) {
        let url = Global1.baseUrl + path;
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if (xhr.status >= 200 && xhr.status < 400) {
                    let response = JSON.parse(xhr.responseText);
                    if (response.code == 200) {
                        callback.call(target, response.data);
                    } else {

                        if (errCB) errCB.call(target);
                        ToastManager.instance.show(`${response.msg}`)
                        // UIUtil.showAlert("获取数据错误:" + path)
                    }
                } else {
                    console.log("status:", xhr.status);
                    // EventManager.sendEvent(Common.LIGHT_TIPS, {str:'请求失败'});
                }
                // GlobalBlock.instance.hide();

            }
        };
        xhr.onerror = () => {
            // GlobalBlock.instance.hide();

            // EventManager.sendEvent(Common.LIGHT_TIPS, {str:'网络错误'});
        }
        xhr.ontimeout = () => {
            // GlobalBlock.instance.hide();

            // EventManager.sendEvent(Common.LIGHT_TIPS, {str:'网络超时'});
        }
        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-Type", "application/json");
        if (Global1.token && Global1.token != "") {
            xhr.setRequestHeader("Authorization", Global1.token);
        }
        let dataStr = JSON.stringify(data);
        xhr.send(dataStr);
        // GlobalBlock.instance.show();

    },
}